package com.dq.controller.op.opmerchant.message;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.InstructionMessageQuery;
import com.dq.vo.Vo;
import com.dq.domain.message.oneself.InstructionMessage;
import com.dq.domain.message.oneself.info.InstructionMessageReturn;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author cf
 */
@Api(description = "指令消息")
@RestController
@RequestMapping("sys/org/instruction/")
public class OrgInstructionMessageController extends AllService {

    @ApiOperation("员工获取boss分配给自己的指令列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:instruction:list")
//    public Vo list(@ApiParam("caseUserId: 关联用户ID") @RequestBody InstructionMessageQuery instructionMessageQuery) throws AdminException {
    public Vo list() throws AdminException {
        InstructionMessageQuery instructionMessageQuery = new InstructionMessageQuery();
        instructionMessageQuery.setStaffUserId(sysUserService.getUser().getId());
        return instructionStaffuserCentreService.getDispose(instructionMessageQuery);
    }

    @ApiOperation("员工回复 boss指令")
    @PostMapping("/reply/{instructionId}")
    @RequiresPermissions("sys:instruction:reply")
    public Vo list(@ApiParam("boss指令ID") @PathVariable Long instructionId, @RequestBody InstructionMessageReturn instructionMessageReturn) throws AdminException {

        InstructionMessage instructionMessage = dao.get(InstructionMessage.class, instructionId);
        if(instructionMessage == null) {
            throw new AdminException("无对应id对象");
        }
        instructionMessageReturn.setInstructionMessage(instructionMessage);

        HashMap<String, Object> query = new HashMap<>(16);
        query.put("createUser_id", sysUserService.getUser().getId());
        query.put("instructionMessage_id", instructionMessageReturn.getInstructionMessage().getId());
        List<InstructionMessageReturn> menuList = dao.find(dao.query2Hql(InstructionMessageReturn.class, query), query);
        if(menuList != null && menuList.size() > 0) {
            InstructionMessageReturn messageReturn = menuList.get(0);
            BeanUtils.copyProperties(instructionMessageReturn, messageReturn, BeanNullUtil.getNullPropertyNames(instructionMessageReturn));
            instructionMessageReturnService.update(messageReturn);
        }else {
            instructionMessageReturnService.save(instructionMessageReturn);
        }

        return new Vo(HttpStatus.OK);
    }

}
